package edu.ccut.saturn.workflow.customUserFinishTaskModule.code;

import java.sql.Connection;
import java.util.List;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;
import edu.ccut.saturn.workflow.model.ActivityInfo;
import edu.ccut.saturn.workflow.model.WorkFlowInfo;
import edu.ccut.saturn.workflow.startWorkFlowModule.code.IParticipantGeter;
import edu.ccut.saturn.workflow.startWorkFlowModule.code.WorkFlowInstanceFactory;

public class SetTaskandParticipant extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		WorkFlowInfo workFlowInfo = (WorkFlowInfo)parameterContext.getParameterValue(0);
		SaturnData workItemInstance = (SaturnData)parameterContext.getParameterValue(1);
		String userSerialNo = (String)parameterContext.getParameterValue(3);
		Connection conn = parameterContext.getConnection();
		//ArrayList<String> userSerialNoList = (ArrayList<String>)parameterContext.getParameterValue(2);
		ActivityInfo currentActivityInfo =workFlowInfo.getActivityInfoById(workItemInstance.getString("activity_define_id"));
		//获得参与者
		IParticipantGeter pg = WorkFlowInstanceFactory.getParticipantGeter(Constants.OBTAINFROMACTIVITYRADIOBUTTON_CHANGE);
		List<SaturnData> participantList = pg.getParticipant(workItemInstance, currentActivityInfo, parameterContext);
	//	SaturnData workItemInstanceChange = getWorkitemInstance(workItemInstance);
		//对于当前任务指定参与者，只有一个任务，不会区分是否多工作项
		//插入工作项
		//Operation.dataBase().insert(workItemInstance, conn);
		for(SaturnData participant :participantList){
			//插入参与者
			participant.set("workitemid", workItemInstance.get("workitem_id"));
			Operation.dataBase().insert(participant, conn);
			//插入代办表
			SaturnData delegateInstance = WorkFlowTools.getDelegateInstance();
			delegateInstance.set("workitem_id", workItemInstance.get("workitem_id"));
			delegateInstance.set("delegate_from", userSerialNo);
			delegateInstance.set("delegate_to", participant.get("participant"));
			delegateInstance.set("start_time", Operation.baseService().GetSystemTime());
			//delegateInstance.set("state", Constants.DELEGATE_STATE_VALID);
			delegateInstance.set("activity_define_id",workItemInstance.get("activity_define_id"));
			delegateInstance.set("process_instance_id",workItemInstance.get("process_instance_id"));
			Operation.dataBase().insert(delegateInstance, conn);
		}
	}

}